package com.jt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.config.TimingConfig;
import com.jt.deepSeaMain.HttpURLConnectionGetResult;
import com.jt.dto.CollectParamsDto;
import com.jt.mapper.PeopleMapper;
import com.jt.mapper.TimingMapper;
import com.jt.pojo.People;
import com.jt.pojo.Result;
import com.jt.pojo.Timing;
import com.jt.service.PeopleService;
import com.jt.utils.FileUtil;
import com.jt.utils.HttpPostAndGetUtil;
import com.jt.utils.OkhttpUtil;
import com.jt.utils.RedisUtils;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

@Slf4j
@RestController
@RequestMapping("/deep")
public class DeepSeaHttpController {

    @Autowired
    private HttpURLConnectionGetResult httpURLConnectionGetResult;
    @Autowired
    private OkhttpUtil okhttpUtil;

    @Autowired
    private RedisUtils redisUtils;


    @Autowired
    private FileUtil fileUtil;

    @Autowired
    private HttpPostAndGetUtil httpPostAndGetUtil;


    @Autowired
    private TimingMapper timingMapper;

    @Autowired
    private PeopleMapper peopleMapper;

    @Autowired
    private PeopleService peopleService;

    /**
     * 获取深海采集数据
     */
    @GetMapping("/collectList")
    public Result getCollectList(@RequestBody CollectParamsDto collectParams) {
        /*调用接口超时处理*/
//        String jsonStr = overtime(collectParams);
        String jsonStr = httpURLConnectionGetResult.getCollectList(collectParams);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String code = jsonObject.get("code").toString();
        if (code.equals("200")) {
            String dataString = jsonObject.get("data").toString();
            return new Result().setCode(200).setData(dataString).setMassage("获取采集接口数据成功");
        }else {
            return new Result().setCode(400).setData(null).setMassage("获取采集接口数据失败");
        }

    }

    @GetMapping("/getToken")
    public Result getNodeToken() throws IOException {
        String token = "";
        String pathUrl = "http://127.0.0.1:32096/deep/token";
        try {
            String result = okhttpUtil.post(pathUrl, null, null);
////            System.out.println("获取接口返回的的token结果值==="+tokenRes);
//            String result = httpPostAndGetUtil.postJson(pathUrl, null, "POST", null);
            System.out.println("获取token接口返回的result的结果值===" + result);
            JSONObject jsonObject = JSON.parseObject(result);
            String access_token = jsonObject.get("access_token").toString();
            System.out.println("获取接口返回access_token===" + access_token);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result().setCode(200).setData(token);
    }


    /*接口调用定时任务*/
    public String overtime(CollectParamsDto collectParams) {
        String ResultString = "";
        ExecutorService executor = Executors.newSingleThreadExecutor();
        FutureTask<String> future = new FutureTask<>(() -> {
            // 调用第三方服务接口获取数据
            String JsonString = httpURLConnectionGetResult.getCollectList(collectParams);
            return JsonString;
        });
        executor.execute(future);
        try {
            ResultString = future.get(10000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            log.error("InterruptedException：", e);
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            log.error("ExecutionException：", e);
        } catch (TimeoutException e) {
            // 10s后超时进行其他业务处理，或者在此处设置个标记返回调用方进行处理
            log.error(">>>>>>>10秒内接口没有返回数据>>>>>>", e.toString());
        } finally {
            future.cancel(true);
            executor.shutdown();
        }
        return ResultString;
    }

}